//
//  YSContentListCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/24.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  内容池子列表

#import "YSContentListCtrl.h"
#import "YSHistoryCell.h"
#import "YSHomeDataTool.h"
#import "YSReviewsCtrl.h"
#import "CartView.h"

@interface YSContentListCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (copy, nonatomic) NSString *reviewId;
@property(nonatomic,strong)CartView *cartView;
@end

@implementation YSContentListCtrl

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (instancetype)initWithReviewsId:(NSString *)reviewId{
    self = [super init];
    if(self){
        self.reviewId = reviewId;
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(_cartView){
        NSInteger num =  [DefaultsHelper integerForKey:kCartIndex];
        [_cartView setNum:num];
    }
    NSLog(@"%d",self.navigationController.navigationBarHidden);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CartView *cartView = [[CartView alloc]initWithImage:@"btn_cart"];
    [cartView addTarget:self action:@selector(tapTitleRightItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:cartView];
    _cartView = cartView;

    [self initTableView];
    [self.tableView.header beginRefreshing];
}

-(void)tapTitleRightItem:(UIBarButtonItem *)btnItem{
    [Utils showCartCtrl:self];
}

-(void)initTableView{
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.header = header;
}

-(void)headerRefresh{
    [self reqeustData];
}

-(void)reqeustData{
    NSLog(@"%@",_reviewId);
    [YSHomeDataTool getContentPoolWithEntityId:_reviewId success:^(YSHistoryRes *result) {
        [UIUtil refreshComplete:self.tableView];
        if(result.isSucc){
            [self setTitle:result.title];
            [self.itemArray removeAllObjects];
            [self.itemArray addObjectsFromArray:result.reviews];
            [self.tableView reloadData];
        }else{
            ShowEnterTips(result.data);
        }

    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSHistoryCell *cell = [YSHistoryCell cellWithTableView:tableView];
    [cell setReviews:self.itemArray[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   return (SCREENW-YSPublicXInset*2)/1.8 +YSPublicXInset+17+20+YSPublicXInset*2;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSReviews *reviews =  self.itemArray[indexPath.row];
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:reviews];
    [self.navigationController pushViewController:ctrl animated:YES];
}

@end
